package ro.gateway.aida.fnd;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import ro.gateway.aida.db.DBPersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.db.OrganizationDB;


/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: BlueOrganizationDB.java,v 1.1 2004/10/24 23:37:14 mihaipostelnicu Exp $
 *
 * @deprecated
 */
public class BlueOrganizationDB extends DBPersistenceManager
{

	private BlueOrganizationDB( PersistenceToken _token )
	{
		  super( _token );
	}

	public static BlueOrganizationDB getManager( PersistenceToken token )
	{
		  return new BlueOrganizationDB( token );
	}

      public BlueOrganization[] getAll()
            throws SQLException
      {

            Connection con = getConnection();
            ArrayList items = new ArrayList();
            BlueOrganization[] result = null;

            try
            {
                  PreparedStatement ps = con.prepareStatement( GET_ALL );
                  ResultSet rs = ps.executeQuery();
                  BlueOrganization last_item = null;
                  while ( rs.next() )
                  {
                        long id = rs.getLong( 1 );
                        if ( ( last_item != null ) && ( last_item.getId() != id ) )
                        {
                              last_item = null;
                        }

                        if ( last_item == null )
                        {
                              last_item = new BlueOrganization();
                              last_item.setId( id );
                              items.add( last_item );
                        }

                        String lang = rs.getString( 2 );
                        last_item.setName( lang, rs.getString( 3 ) );
                        last_item.setDescription( lang, rs.getString( 4 ) );
                  }
                  rs.close();
                  ps.close();
            }
            catch ( SQLException sqlEx )
            {
                  throw sqlEx;
            }
            finally
            {
                  con.close();
            }

            if ( items.size() > 0 )
            {
                  result = new BlueOrganization[ items.size() ];
                  items.toArray( result );
            }

            return result;
      }

  public  BlueOrganization getById(
            long id )
            throws SQLException
      {
  			return new BlueOrganization
			(OrganizationDB.getManager(token).getByID(id));
      }

 

            public static final String GET_ALL =
            "SELECT o.id, od.lang, od.name, od.description " +
            "FROM orgs o, orgs_desc od " +
            "WHERE o.id=od.id " +
            "ORDER BY o.id ASC";

      public static final String GET_BY_ID =
            "SELECT o.id, od.lang, od.name, od.description " +
            "FROM orgs o, orgs_desc od " +
            "WHERE o.id=od.id AND o.id=? " +
            "ORDER BY o.id ASC";
}
